<?php

namespace App\Imports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class ExcelImport implements ToCollection
{
    public $data;
    
    protected $del_title;
    /**
     * 
     * @param number $del_title //去掉几行标题  默认一行
     */
    public function __construct($del_title = 1){
        $this->del_title = $del_title;
    }
    /**
    * @param Collection $collection
    */
    public function collection(Collection $rows)
    {
        $list = $rows->toArray();
        $count = 0;
        foreach($list[0] as $v){
            if(!empty($v)){
                $count++;
            }else{
                break;
            }
        }
        $this->delTitle($rows);
        $list = $rows->toArray();
        $data = [];
        foreach($list as $k=>$v){
            for($i = 0;$i < $count;$i++){
                if(empty($v[0])){
                    break;
                }else{
                    $data[$k][$i] = $v[$i];
                }
            }
        }
        $this->data = $data;
    }
    
    public function delTitle(&$rows){
        $rows = $rows->slice($this->del_title)->values();
    }
}
